var t = getApp(), e = 0, a = null;

Page({
    data: {
        language: "",
        card_id: "",
        chooseItem: [],
        showStart: !1,
        localId: "",
        serverId: "",
        downLoadId: "",
        time: 0,
        timer: null,
        pictures: [],
        form: {
            status: "1",
            member_id: "",
            card_avatar: "",
            card_name: "",
            card_mobile: "",
            card_wechat: "",
            role_name: "",
            role_id: "",
            introduction: "",
            voice: "",
            province: "",
            city: "",
            district: "",
            street: "",
            address: "",
            picture: [],
            goods_id: [],
            addressName: "",
            streetName: "",
            isOldVoice: !1,
            company: "",
            wechat_type: "0",
            wechat_qrcode: "",
            role_type: "0"
        },
        role_nameDiy: "",
        addressShow: !1,
        strShow: !1,
        streetShow: !1,
        showRole: !1,
        columns: [],
        role: {},
        photosize: 4194304,
        imageUrl: "",
        fileList2: [],
        Timestart: 0,
        onFocus: !1,
        isShowText: !1
    },
    onLoad: function(t) {
        this.setData({
            card_id: t.card_id
        }), t.isShow ? this.setData({
            isShow: t.isShow
        }) : this.setData({
            isShow: !1
        }), this.getData(), this.getRole();
        var e = this;
        this.recorderManager = wx.getRecorderManager(), this.recorderManager.onError(function() {
            e.tip("录音失败！");
        }), this.recorderManager.onStop(function(t) {
            e.setData({
                src: t.tempFilePath,
                localId: t.tempFilePath
            }), console.log(t.tempFilePath, "上传的路径"), e.tip("录音完成！"), e.upVoice();
        }), this.innerAudioContext = wx.createInnerAudioContext(), this.innerAudioContext.onError(function(t) {
            e.tip("播放录音失败！");
        });
    },
    bindRegionChange: function(t) {
        console.log(t), "441900" == t.detail.code[1] ? (t.detail.value[2] = "东莞市", t.detail.code[2] = "659004523", 
        this.setData({
            region: t.detail.value
        })) : (this.setData({
            "form.province": t.detail.code[0],
            "form.city": t.detail.code[1],
            "form.district": t.detail.code[2]
        }), this.setData({
            region: t.detail.value
        })), this.setData({
            addressShow: !1,
            "form.addressName": t.detail.value[0] + t.detail.value[1] + t.detail.value[2],
            "form.streetName": "",
            "form.street": ""
        }), this.getStreet(t.detail.code[2]);
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        }), this.audioCtx = wx.createAudioContext("yp");
    },
    onShow: function() {
        this.setData({
            role_nameDiy: ""
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多了");
    },
    onShareAppMessage: function() {},
    tip: function(t) {
        wx.showModal({
            title: "提示",
            content: t,
            showCancel: !1
        });
    },
    checkRole: function() {
        0 == this.data.form.role_type && this.setData({
            showRole: !0
        });
    },
    checkoutAdress: function() {
        this.setData({
            addressShow: !0
        });
    },
    onCancel: function() {
        this.setData({
            showRole: !1
        });
    },
    onConfirm: function(t) {
        for (var e in this.data.columnsObj) this.data.columnsObj[e] == t.detail.value && this.setData({
            showRole: !1,
            "form.role_name": t.detail.value,
            "form.role_id": e
        });
    },
    checkClose: function() {
        this.setData({
            addressShow: !1
        });
    },
    checkClick: function(t) {
        var e = t.detail.values, a = "";
        e.forEach(function(t) {
            a += t.name;
        }), this.setData({
            "form.province": e[0].code,
            "form.city": e[1].code,
            "form.district": e[2].code
        }), this.getStreet(e[2].code), this.setData({
            addressShow: !1,
            "form.addressName": a,
            "form.streetName": "",
            "form.street": ""
        });
    },
    onRead: function(t) {
        console.log(t);
        var e = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "original" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                var o = a.tempFilePaths;
                "wxcode" == t.currentTarget.dataset.key ? e.upload(e, o, t.currentTarget.dataset.key) : e.upload(e, o);
            }
        });
    },
    upload: function(e, a, o) {
        var s = this, r = wx.getStorageSync("wx_token"), i = wx.getStorageSync("session"), n = t.getNetAddresss("plugin.business-card.frontend.controllers.card.card.upload");
        n += "&3rd_session=" + i, wx.showToast({
            icon: "loading",
            title: "正在上传"
        }), wx.uploadFile({
            url: n,
            filePath: a[0],
            name: "file",
            formData: {
                _wx_token: wx.getStorageSync("wx_token")
            },
            header: {
                "Content-Type": "multipart/form-data",
                Cookie: "PHPSESSID=" + r
            },
            success: function(t) {
                var e = JSON.parse(t.data);
                1 == e.result ? "wxcode" == o ? s.setData({
                    "form.wechat_qrcode": e.data.img_url
                }) : s.setData({
                    "form.card_avatar": e.data.img_url
                }) : wx.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1
                });
            },
            fail: function(t) {
                console.log(t), wx.showModal({
                    title: "提示",
                    content: "上传失败",
                    showCancel: !1
                });
            },
            complete: function() {
                wx.hideToast();
            }
        });
    },
    onRead_1: function() {
        var t = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "original" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                var a = e.tempFilePaths;
                t.upload2(t, a);
            }
        });
    },
    upload2: function(e, a) {
        var o = this, s = wx.getStorageSync("wx_token"), r = wx.getStorageSync("session"), i = t.getNetAddresss("plugin.business-card.frontend.controllers.card.card.upload");
        i += "&3rd_session=" + r, wx.showToast({
            icon: "loading",
            title: "正在上传"
        }), wx.uploadFile({
            url: i,
            filePath: a[0],
            name: "file",
            formData: {
                _wx_token: wx.getStorageSync("wx_token")
            },
            header: {
                "Content-Type": "multipart/form-data",
                Cookie: "PHPSESSID=" + s
            },
            success: function(t) {
                var a = JSON.parse(t.data);
                if (1 == a.result) {
                    var s = o.data.form.picture;
                    s.push(a.data.img_url), e.setData({
                        "form.picture": s,
                        pictures: s
                    });
                } else wx.showModal({
                    title: "提示",
                    content: "上传失败",
                    showCancel: !1
                });
            },
            fail: function(t) {
                console.log(t), wx.showModal({
                    title: "提示",
                    content: "上传失败",
                    showCancel: !1
                });
            },
            complete: function() {
                wx.hideToast();
            }
        });
    },
    removeImg: function(t) {
        var e = t.target.dataset.index || t.currentTarget.dataset.index, a = this.data.form.picture;
        a.splice(e, 1), this.setData({
            "form.picture": a
        });
    },
    getData: function() {
        var e = this, a = t.getNetAddresss("plugin.business-card.frontend.controllers.card.card.index");
        a += "&card_id=" + this.data.card_id, t._getNetWork({
            url: a,
            success: function(a) {
                var o = a.data;
                if (1 == o.result) {
                    var s = o.data.card;
                    if (t._isTextEmpty(s.voice) || "-1" != s.voice.indexOf("amr") ? e.setData({
                        isOldVoice: !1
                    }) : e.setData({
                        isOldVoice: !0,
                        "form.voice": s.voice
                    }), e.options.type) {
                        e.setData({
                            "form.status": s.status,
                            "form.member_id": s.member_id,
                            "form.card_avatar": s.card_avatar,
                            "form.card_name": s.card_name,
                            "form.card_mobile": s.card_mobile,
                            "form.card_wechat": s.card_wechat,
                            "form.role_name": s.role_name,
                            "form.role_id": s.role_id,
                            "form.introduction": s.introduction,
                            "form.address": s.address,
                            "form.company": s.company,
                            "form.role_type": String(s.role_type),
                            "form.wechat_type": String(s.wechat_type),
                            "form.wechat_qrcode": s.wechat_qrcode
                        }), 1 == e.data.form.role_type && e.setData({
                            role_nameDiy: s.role_name
                        });
                        var r = e.data.form.status;
                        r = r.toString(), e.setData({
                            "form.status": r
                        }), s.areas[0] && e.setData({
                            "form.addressName": s.areas[0] + s.areas[1] + s.areas[2]
                        }), s.areas[3] && e.setData({
                            "form.streetName": s.areas[3]
                        }), e.setData({
                            "form.province": s.area_id[0],
                            "form.city": s.area_id[1],
                            "form.district": s.area_id[2],
                            "form.street": s.area_id[3],
                            "form.address ": s.address
                        }), s.area_id[2] && e.getStreet(s.area_id[2]), s.picture ? e.setData({
                            "form.picture": s.picture
                        }) : e.setData({
                            "form.picture": []
                        }), e.setData({
                            chooseItem: o.data.card_goods
                        }), wx.setStorage({
                            key: "chooseItem",
                            data: e.data.chooseItem
                        });
                    } else t._isTextEmpty(e.data.form.street) && e.getStreet(e.data.form.district), 
                    wx.getStorage({
                        key: "chooseItem",
                        success: function(t) {
                            e.setData({
                                chooseItem: t.data
                            });
                        }
                    }), wx.getStorage({
                        key: "cardData",
                        success: function(t) {
                            e.setData({
                                form: t.data
                            });
                        }
                    });
                } else wx.showToast({
                    title: o.msg,
                    icon: "none"
                }), setTimeout(function() {
                    wx.reLaunch({
                        url: "/packageG/index/index"
                    });
                }, 2e3);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getRole: function() {
        var e = this, a = t.getNetAddresss("plugin.business-card.frontend.controllers.card.card.getRoles");
        a += "&card_id=" + this.data.card_id, t._getNetWork({
            url: a,
            success: function(t) {
                var a = t.data;
                if (1 == a.result) {
                    var o = [];
                    e.setData({
                        role: a.data
                    }), Object.keys(a.data).forEach(function(t) {
                        o.push(a.data[t]);
                    }), e.setData({
                        columns: o,
                        columnsObj: a.data
                    });
                } else wx.showToast({
                    title: a.msg,
                    icon: "none"
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    streetChoose: function(e) {
        t._isTextEmpty(this.data.form.addressName) ? wx.showToast({
            title: "请先选择所在地区",
            icon: "none"
        }) : this.setData({
            streetShow: !0
        });
    },
    streetConfirm: function(t) {
        var e = t.target.dataset.item || t.currentTarget.dataset.item;
        this.setData({
            "form.streetName": e.areaname,
            "form.street": e.id,
            streetShow: !1
        });
    },
    getStreet: function(e) {
        var a = this, o = t.getNetAddresss("member.memberAddress.street");
        o += "&district_id=" + e, t._getNetWork({
            url: o,
            success: function(e) {
                var o = e.data;
                1 != o.result || t._isTextEmpty(o.data) ? (1 == o.result && t._isTextEmpty(o.data) || wx.showToast({
                    title: o.msg,
                    icon: "none"
                }), a.setData({
                    strShow: !1
                })) : !t._isTextEmpty(o.data) && o.data.length > 0 ? a.setData({
                    districtVal: o.data,
                    strShow: !0
                }) : a.setData({
                    strShow: !1
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    setJSONData: function() {
        var t = this.data.card_id;
        this.setData({
            "form.card_id": t
        });
        for (var e = [], a = 0; a < this.data.chooseItem.length; a++) e.push(this.data.chooseItem[a].id);
        this.setData({
            "form.goods_id": e
        }), "1" != this.data.form.role_id && "2" != this.data.form.role_id && "3" != this.data.form.role_id && "6" != this.data.form.role_id && this.setData({
            "form.goods_id": []
        });
    },
    deleteGoods: function(t) {
        for (var e = t.target.dataset.id || t.currentTarget.dataset.id, a = this.data.chooseItem, o = 0; o < a.length; o++) a[o].id == e && a.splice(o, 1);
        this.setData({
            chooseItem: a
        }), wx.setStorage({
            key: "chooseItem",
            data: this.data.chooseItem
        });
    },
    saveData: function(e) {
        var a = this;
        if (this.data.form.card_name) {
            this.setJSONData(), 1 == this.data.form.role_type && (this.data.form.role_name = this.data.role_nameDiy), 
            "441903" == this.data.form.district && (this.data.form.district = "659004523"), 
            console.log(this.data.form.district);
            var o = t.getNetAddresss("plugin.business-card.frontend.controllers.card.card.edit"), s = this.data.form;
            wx.setStorage({
                key: "streetName",
                data: s.streetName
            }), t._postNetWork({
                url: o,
                data: s,
                success: function(t) {
                    var e = t.data;
                    1 == e.result ? (wx.setStorage({
                        key: "cardData",
                        data: ""
                    }), wx.setStorage({
                        key: "chooseItem",
                        data: ""
                    }), 0 == a.data.isShow ? wx.navigateBack({
                        delta: 1
                    }) : wx.navigateBack({
                        delta: 4
                    })) : wx.showToast({
                        title: e.msg,
                        icon: "none"
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else wx.showToast({
            title: "请输入姓名",
            icon: "none"
        });
    },
    onChange1: function(t) {
        console.log(t.detail.value), this.setData({
            role_nameDiy: t.detail.value
        });
    },
    groupChange: function(t) {
        this.setData({
            "form.wechat_type": t.detail
        });
    },
    roleChange: function(t) {
        this.setData({
            "form.role_type": t.detail,
            "form.role_name": "",
            role_nameDiy: ""
        });
    },
    onChangeName: function(t) {
        this.setData({
            "form.card_name": t.detail.value
        }), console.log(this.data.form.card_name, "name");
    },
    onChangePhone: function(t) {
        this.setData({
            "form.card_mobile": t.detail.value
        });
    },
    onChangeWechat: function(t) {
        this.setData({
            "form.card_wechat": t.detail.value
        });
    },
    onChangeCompany: function(t) {
        console.log(t), this.setData({
            "form.company": t.detail.value
        });
    },
    onChangeAddress: function(t) {
        this.setData({
            "form.address": t.detail.value
        });
    },
    onChangeRadio: function(t) {
        var e = t.detail.toString();
        this.setData({
            "form.status": e
        });
    },
    onShowTextare: function() {
        this.setData({
            isShowText: !0,
            onFacus: !0
        });
    },
    onShowText: function() {
        this.setData({
            isShowText: !1,
            onFacus: !1
        });
    },
    changeLine: function(t) {
        this.setData({
            "form.introduction": t.detail.value
        });
    },
    start: function() {
        var t = this;
        this.setData({
            showStart: !0
        }), this.recorderManager.start({
            format: "mp3"
        }), e = 0, a = setInterval(function() {
            e++, t.setData({
                time: e
            });
        }, 1e3);
    },
    reloadVoice: function() {
        this.setData({
            isOldVoice: !1
        });
    },
    stop: function() {
        this.setData({
            showStart: !1
        }), clearInterval(a), this.setData({
            time: e
        }), this.recorderManager.stop();
    },
    play: function() {
        t._isTextEmpty(this.data.form.voice) ? this.tip("请先录音！") : (this.innerAudioContext.src = this.data.form.voice, 
        this.innerAudioContext.play());
    },
    playAmr: function() {
        this.play();
    },
    upVoice: function() {
        var e = this, a = wx.getStorageSync("wx_token"), o = wx.getStorageSync("session"), s = t.getNetAddresss("plugin.business-card.frontend.controllers.card.card.uploadAudio");
        s += "&3rd_session=" + o, wx.showToast({
            icon: "loading",
            title: "正在上传"
        }), wx.uploadFile({
            url: s,
            filePath: this.data.localId,
            name: "file",
            header: {
                "Content-Type": "multipart/form-data",
                Cookie: "PHPSESSID=" + a
            },
            formData: {
                _wx_token: wx.getStorageSync("wx_token")
            },
            success: function(t) {
                var a = JSON.parse(t.data);
                1 == a.result ? (wx.showModal({
                    title: "提示",
                    content: a.msg,
                    showCancel: !1
                }), e.setData({
                    "form.voice": a.data.audio_url
                })) : wx.showModal({
                    title: "提示",
                    content: a.msg || "错误",
                    showCancel: !1,
                    success: function(t) {}
                }), wx.hideToast();
            },
            fail: function(t) {
                console.log(t), wx.showModal({
                    title: "提示",
                    content: "网络请求失败，请确保网络是否正常",
                    showCancel: !1,
                    success: function(t) {}
                });
            }
        });
    },
    toChooseGood: function() {
        wx.setStorage({
            key: "cardData",
            data: this.data.form
        }), wx.navigateTo({
            url: "/packageC/microshop/micro_shop_add_category/micro_shop_add_category?formCard=" + this.data.card_id
        });
    },
    onClickLeft: function() {
        this.setData({
            streetShow: !1
        });
    },
    areaConfirm: function(t) {
        console.log(t), this.setData({
            addressShow: !1,
            "form.province": t.detail[0].id,
            "form.city": t.detail[1].id,
            "form.district": t.detail[2].id,
            "form.addressName": t.detail[0].name + t.detail[1].name + t.detail[2].name,
            "form.streetName": "",
            "form.street": ""
        }), this.getStreet(t.detail[2].id);
    }
});